//
//  MineAboutTableViewController.m
//  MJApp
//
//  Created by wangliang on 15/3/30.
//  Copyright (c) 2015年 wangliang. All rights reserved.
//

#import "MineAboutTableViewController.h"
#import "MineAboutTableViewCell.h"
#import "MineExitTableViewCell.h"
#import "SDImageCache.h"

@interface MineAboutTableViewController ()

@property(nonatomic,strong)NSArray *tableArray;

@end

@implementation MineAboutTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self Data];
    [self UI];
}

- (void)Data {
    
    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    NSString *version = infoDic[@"CFBundleShortVersionString"];
    CGFloat cacheSize = [[SDImageCache sharedImageCache] getSize] / 1024.0;
    NSString *sizeString = cacheSize > 1024 ?
    [NSString stringWithFormat:@"%0.2fM",cacheSize/1024] :
    [NSString stringWithFormat:@"%0.0fKB",cacheSize];
    self.tableArray = @[
    @[
      @{@"name":@"版本信息",@"desc":version,@"end":@"",@"new":@"yes"},
      @{@"name":@"缓存清理",@"desc":@"",@"end":sizeString,@"new":@""},
      @{@"name":@"邀请下载",@"desc":@"",@"end":@"",@"new":@""}
      ],
    @[@{@"name":@"关于我们",@"desc":@"",@"end":@"",@"new":@""}]
    /*@[@{@"name":@"退出当前账号",@"desc":@"",@"end":@"",@"new":@""}]*/];
}

- (void)UI {
    
    [(TabBarController *)self.tabBarController setTabbarHidden:YES];
    self.title = @"设置";
    self.tableView.backgroundColor = Color_Back_Black;
    self.tableView.separatorColor = [UIColor clearColor];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.tableHeaderView.backgroundColor = Color_Back_Black;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return [userInfoTool share].token ? self.tableArray.count : self.tableArray.count - 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [self.tableArray[section] count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 20;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 2) {
        
        MineExitTableViewCell *cell = (MineExitTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
        return cell.cellHigh;
    }
    MineAboutTableViewCell *cell = (MineAboutTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.cellHigh;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 2) {
        
        MineExitTableViewCell *cell = [MineExitTableViewCell initWithTableView:tableView];
        cell.infoDic = self.tableArray[indexPath.section][indexPath.row];
        return cell;
    }
    MineAboutTableViewCell *cell = [MineAboutTableViewCell initWithTableView:tableView];
    cell.infoDic = self.tableArray[indexPath.section][indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 2) {
        
        [userInfoTool updateInfo:nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:K_NOTICE_MINE object:nil];
        [[DMCAlertCenter defaultCenter] postAlertWithMessage:@"成功退出"];
        [self.tableView reloadData];
    }else if (indexPath.section == 0 && indexPath.row == 1) {
        
        [[SDImageCache sharedImageCache] clearDisk];
        [self Data];
        [tableView reloadData];
    }
}


@end
